/*** DigiInterface Class
 * 		Matthew Smith
 * 		http://digivation.net/programming/
 * 
 * 		Description: Provides methods to create an IE interface!
 * 		Note: this class is not complete! It just does the stuff
 * 		I needed, but later versions may expand on this
 * 
 * 		Version: 0.1
 */

var ieWindowClosed = false; // this must be a global variable

function digiInterface(width,height,title) {
	// create new automation object for Internet Explorer
	var ieObject = WScript.CreateObject("InternetExplorer.Application","ieWin_");
	
	// set up some default options to get IE as plain as possible
	ieObject.Navigate("about:blank");
	ieObject.AddressBar = false;
	ieObject.MenuBar = false;
	ieObject.Resizable = false;
	ieObject.StatusBar = false;
	ieObject.ToolBar = false;

	// set height and width
	ieObject.Height = height;
	ieObject.Width = width;
	
	// wait for window to be ready
	while(ieObject.readyState != 4) {
		WScript.Sleep(100);
	}
	
	var thePage = ieObject.document;
	
	// create the base page with callable page element
	thePage.write("<html><title>"+ title + "</title><style>body{ margin:0px; font-family: sans-serif; font-size: smaller; }</style><body>\n");
	thePage.write("<div id=\"page\"></div>\n");
	thePage.write("</body></html>");
	
	// display the window (use the functions below to display when ready)
	//ieObject.Visible = true;
	
	
	// methods
	this.showWindow = function(){
		ieObject.Visible = true;
	}
	
	this.hideWindow = function(){
		ieObject.Visible = false;
	}
	
	this.createText = function(text,leftPos,topPos,width,height){
		var page = thePage.getElementById("page");
		var blockID = "textBlock_" + generateString(5); // get random ID

		// build text block
		// insert top of div
		page.insertAdjacentHTML("beforeEnd", "<div id="+blockID+" style=\"position: absolute; top: " + topPos + "; left: " + leftPos +"; width: " + width + "; height: " + height + ";\">")
		// insert end of div
		page.insertAdjacentHTML("beforeEnd", "</div>");
		
		// insert content
		block = thePage.getElementById(blockID);
		block.insertAdjacentHTML("beforeEnd", text);
		
		// return handle so that the block can be referenced
		return blockID;
	}
	
	this.addText = function(blockID, text){
		var block = thePage.getElementById(blockID);
		block.insertAdjacentHTML("beforeEnd", text);
	}
	
	this.createButton = function(btnTitle, callFn, leftPos, topPos){
		var buttonID = "button_"+generateString(5); // generate id
		var page = thePage.getElementById("page");
		
		// determine relative or absolute positioning
		if(!leftPos | !topPos){
			page.insertAdjacentHTML("beforeEnd", "<button id=\""+buttonID+"\">"+btnTitle+"</button>"); // create button
		} else {
			page.insertAdjacentHTML("beforeEnd", "<button id=\""+buttonID+"\" style=\"position: absolute; top: " + topPos + "; left: " + leftPos +"; \">"+btnTitle+"</button>"); // create button
		}
		
		button = thePage.getElementById(buttonID); // get object for create button
		
		// assign the onclick function
		button.onclick = function(){
			eval(callFn);
		}
		
		return buttonID; // return our id in case we need to reference the button later
	}
	
	this.createTextField = function(defText,leftPos,topPos) {
		var textID = "text_"+generateString(5);
		var page = thePage.getElementById("page");
		
		page.insertAdjacentHTML("beforeEnd", "<input type=\"text\" name=\"" + textID + "\" value=\"" + defText + "\" style=\"position: absolute; top: " + topPos + "; left: " + leftPos + ";\">")
		
		return textID;
	}
	
	this.createCheckOptions = function(options) {
		
	}
	
	this.writeRawHTML = function(html) {
		var page = thePage.getElementById("page");
		page.insertAdjacentHTML("beforeEnd",html);
	}
	
	this.getElementById = function(id) {
		return thePage.getElementById(id);
	}
}


// window close handler
function ieWin_OnQuit() {
	ieWindowClosed = true;
}

// the following functions are used to create random strings
// for the element ids

// string generation function. found on the web (NOT MY CODE)
function generateString(slen) {
	// Define local variables.
	var intI = 0;
	var strLargeText = "";
	var intValue = 0;
	var arrCharacters = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
	 
	// Loop over number of characters in string.
	for (intI = 0 ; intI < slen ; intI++){
		// Get a random value between 0 and the length of the
		// character list.
		intValue = RandRange( 0, (arrCharacters.length - 1), intI );
		 
		// Append a character that is randomly chosen
		strLargeText += arrCharacters[ intValue ];
	}
	
	return strLargeText;
}

// random number in a range function, also found on the web.
function RandRange( intFrom, intTo, intSeed ){
	// Make sure that we have integers.
    intFrom = Math.floor( intFrom );
    intTo = Math.floor( intTo );
     
    // Return the random number.
    return(
    	Math.floor(intFrom + ( (intTo - intFrom + 1)*Math.random((intSeed != null) ? intSeed : 0))));
	}